import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { Picker, Toast } from 'ng-zorro-antd-mobile';
import { log } from 'util';
import { DataBulk } from './data-detail/data-detail';
import { Router } from '@angular/router';

@Component({
    selector: 'app-house-loan',
    templateUrl: './house-loan.component.html',
    styleUrls: ['./house-loan.component.scss'],
    providers: [Picker, Toast]
})
export class HouseLoanComponent implements OnInit {

    oData = {};

    loanTypes = '';
    // 是否禁用公积金/商业贷款
    isdisabled3: Boolean = false;
    isdisabled5: boolean = true;
    // 是否显示公积金/商业贷款
    isshowbusiness = true;
    isshowggj = true;
    // 房价总额
    total: number = 100;
    loanCeiling: number = 70;

    // select组件数据
    singleArea1 = ['25%', '30%', '40%', '50%', '60%', '70%', '80%', '90%'];
    singleArea2 = ['组合贷款', '商业贷款', '公积金贷款'];
    singleArea3 = [];
    singleArea4 = DataBulk.selecInterest;
    singleArea5 = []
    singleArea6 = ['最新基准利率(3.25%)', '1.1倍(3.575%)', '1.2倍(3.9%)', '1.3倍(4.225%)'];

    value = [];

    name1 = '30%(30万)';
    value1: number = 30;   // parseFloat(30%)    

    name2 = '组合贷款';
    value2 = '组合贷款';

    name3 = '70万 20年(24期)';
    value3 = [];
    value3Money: number = 70;
    value3Year: number = 20;  // parseFloat(1年12(期)) 

    name4 = '最新基准利率(4.9%)';
    value4: number = 4.9;   // parseFloat(4.9%)

    name5 = '0万 20年(24期)';
    value5 = [];
    value5Money: number = 0;
    value5Year: number = 20; // parseFloat(1年12(期)) 

    name6 = '最新基准利率(3.25%)';
    value6: number = 3.25;  // parseFloat(3.25%)

    constructor(
        private location: Location,
        private _picker: Picker,
        private router: Router,
        private _toast: Toast) { }
    
    ngOnInit() {
        this.singleArea3 = this.businessData();
        this.singleArea5 = [{
            label: this.value5Money + '万',
            children:  [{ label: '20年(24期)' }]
        }]
    }
    ngAfterViewInit() { }
    ngOnDestroy() { } // 销毁指令/组件前调用
    // 点击返回
    onClickLeft() {
        this.location.back()
    }
    // 点击计算
    sumbit() {

        if(this.total<=0){

            Toast.fail('请输入房价总额', 2000);
        } else {
            switch (this.value2) {
                case '组合贷款': 
                    this.loanTypes = 'Mix';
                    break;
                case '商业贷款': 
                    this.loanTypes = 'Business'
                    break;
                case '公积金贷款': 
                    this.loanTypes = 'Fund'
                    break;
                default:
                    break;
            }
            
            let oData = {
    
                total: this.total,  // 总价
                type: this.loanTypes,
                money: this.total-this.loanCeiling,
                businessLoan: JSON.stringify({ 
                    Money: { Name: "0万", Value: this.value3Money }, 
                    Year: { Name: "20年(240期)", Value: this.value3Year } 
                }),
                businessLoanRate: JSON.stringify({
                     Name: "最新基准利率 (4.9%)", Value: this.value4
                }),
                fundLoan: JSON.stringify({ 
                    Money: { Name: "0万", Value: this.value5Money }, 
                    Year: { Name: "20年(240期)", Value: this.value5Year } 
                }),
                fundLoanRate: JSON.stringify({
                     Name: "最新基准利率 (4.9%)", Value: this.value6 
                })  
            }
            // console.log(oData);
            this.router.navigate(["/workspace/mine/loanDetail"],{ queryParams: oData });
        }
    }
    omblurInput() {
        if (!this.total) {
            Toast.fail('请输入房价总额', 2000);
            this.inputClick(100)
        }
        return
    }
    // 改变房价总额
    inputClick(e) {

        this.total = e

        this.name1 = parseFloat(this.name1) + '%(' + Math.round(this.total * ((parseFloat(this.name1) / 100))) + '万)'
        this.value1 = parseFloat(this.name1)

        // 贷款总额
        this.loanCeiling = Math.round(this.total - this.total*(parseFloat(this.name1)/100));
        if (this.name2 == "公积金贷款") {

            this.name5 = this.loanCeiling - this.value3Money + '万' + this.tostring(this.name5)
            this.value5Money = this.loanCeiling - this.value3Money
            this.value5Year = parseFloat(this.tostring(this.name5))

            this.name3 = this.loanCeiling - this.value5Money + '万' + this.tostring(this.name3)
            this.value3Money = this.loanCeiling - this.value5Money
            this.value3Year = parseFloat(this.tostring(this.name3))

            this.singleArea5 = [{
                label: this.value5Money + '万',
                children: DataBulk.selecYear
            }]
            this.singleArea3 = [{
                label: 0 + '万',
                children: DataBulk.selecYear
            }]

        } else if (this.name2 == "商业贷款") {

            this.name3 = this.loanCeiling - this.value5Money + '万' + this.tostring(this.name3)
            this.value3Money = this.loanCeiling - this.value5Money
            this.value3Year = parseFloat(this.tostring(this.name3))


            this.name5 = this.loanCeiling - this.value3Money + '万' + this.tostring(this.name5)
            this.value5Money = this.loanCeiling - this.value3Money
            this.value5Year = parseFloat(this.tostring(this.name5))

            this.singleArea3 = [{
                label: this.value3Money + '万',
                children: DataBulk.selecYear
            }]
            this.singleArea5 = [{
                label: 0 + '万',
                children: DataBulk.selecYear
            }]
        // 组合贷款
        } else {

            this.name3 = this.loanCeiling + '万 20年(24期)'
            this.value3Money = this.loanCeiling
            this.value3Year = parseFloat(this.tostring(this.name3))

            this.name5 = this.loanCeiling - this.value3Money + '万 20年(24期)'
            this.value5Money = this.loanCeiling - this.value3Money
            this.value5Year = parseFloat(this.tostring(this.name5))

            this.singleArea3 = this.businessData()
            // this.singleArea5 = this.gjjData()
            this.singleArea5 = [{
                label: this.value5Money + '万',
                children:  [{ label: '20年(24期)' }]
            }]
        }
        
    }
    // // 首付选择
    onOk1(result) {

        this.value1 = parseFloat(result[0])
        this.name1 = result[0] + '(' + Math.round(this.total * (parseFloat(result[0]) / 100)) + '万)'
        // 贷款总额
        this.loanCeiling = Math.round(this.total -this.tonumber(this.name1) );

        if (this.name2 == "公积金贷款") {

            this.name5 = this.loanCeiling - this.value3Money + '万' + this.tostring(this.name5)
            this.value5Money = this.loanCeiling - this.value3Money
            this.value5Year = parseFloat(this.tostring(this.name5))

            this.name3 = this.loanCeiling - this.value5Money + '万' + this.tostring(this.name3)
            this.value3Money = this.loanCeiling - this.value5Money
            this.value3Year = parseFloat(this.tostring(this.name3))

            this.singleArea5 = [{
                label: this.loanCeiling + '万',
                children: DataBulk.selecYear
            }]
            this.singleArea3 = [{
                label: 0 + '万',
                children: DataBulk.selecYear
            }]

        } else if (this.name2 == "商业贷款") {

            this.name3 = this.loanCeiling - this.value5Money + '万' + this.tostring(this.name3)
            this.value3Money = this.loanCeiling - this.value5Money
            this.value3Year = parseFloat(this.tostring(this.name3))


            this.name5 = this.loanCeiling - this.value3Money + '万' + this.tostring(this.name5)
            this.value5Money = this.loanCeiling - this.value3Money
            this.value5Year = parseFloat(this.tostring(this.name5))

            this.singleArea3 = [{
                label: this.value3Money + '万',
                children: DataBulk.selecYear
            }]
            this.singleArea5 = [{
                label: 0 + '万',
                children: DataBulk.selecYear
            }]
        // 组合贷款
        } else {

            this.name3 = this.loanCeiling + '万 20年(24期)'
            this.value3Money = this.loanCeiling
            this.value3Year = parseFloat(this.tostring(this.name3))

            this.name5 = this.loanCeiling - this.value3Money + '万 20年(24期)'
            this.value5Money = this.loanCeiling - this.value3Money
            this.value5Year = parseFloat(this.tostring(this.name5))

            this.singleArea3 = this.businessData()
            // this.singleArea5 = this.gjjData()
            this.singleArea5 = [{
                label: this.value5Money + '万',
                children:  [{ label: '20年(24期)' }]
            }]
        }

    }
    // 贷款方式
    onOk2(result) {

        this.name2 = result[0];
        this.value2 = result[0];
        if (result[0] == "组合贷款") {

            this.isdisabled3 = false;
            this.isdisabled5 = true;
            this.isshowbusiness = true;
            this.isshowggj = true;

            this.name1 = parseFloat(this.name1) + '%(' + Math.round(this.total * ((parseFloat(this.name1) / 100))) + '万)'
            this.value1 = parseFloat(this.name1)

            this.name3 = this.loanCeiling + '万 20年(24期)'
            this.value3Money = JSON.parse(JSON.stringify(this.loanCeiling))
            this.value3Year = parseFloat(this.tostring(this.name3))

            this.name5 = 0 + '万 20年(24期)'
            this.value5Money = this.loanCeiling - this.value3Money
            this.value5Year = parseFloat(this.tostring(this.name5))

            this.singleArea3 = this.businessData()
            // this.singleArea5 = this.gjjData()
            this.singleArea5 = [{
                label: this.value5Money + '万',
                children: [{label: '20年(24期)' }]
            }]

        } else if (result[0] == "商业贷款") {

            this.isdisabled3 = false;
            this.isdisabled5 = true;
            this.isshowbusiness = true;
            this.isshowggj = false;

            this.value1 = parseFloat(this.name1)
            this.name1 = parseFloat(this.name1) + '%(' + Math.round(this.total * ((parseFloat(this.name1) / 100))) + '万)'

            this.name5 = ''
            this.value5Money = 0
            this.value5Year = 0

            this.name3 = this.loanCeiling + '万 20年(24期)'
            this.value3Money = JSON.parse(JSON.stringify(this.loanCeiling))
            this.value3Year = parseFloat(this.tostring(this.name3))

            this.singleArea3 = [{
                label: this.loanCeiling + '万',
                children: DataBulk.selecYear
            }]
            this.singleArea5 = [{
                label: 0 + '万',
                children: DataBulk.selecYear
            }]
        // 公积金贷款
        } else {

            this.isdisabled3 = true;
            this.isdisabled5 = false;
            this.isshowbusiness = false;
            this.isshowggj = true;

            this.value1 = parseFloat(this.name1)
            this.name1 = parseFloat(this.name1) + '%(' + Math.round(this.total * ((parseFloat(this.name1) / 100))) + '万)'

            this.name3 = ''
            this.value3Money = 0
            this.value3Year = 0

            this.name5 = this.loanCeiling + '万 20年(24期)'
            this.value5Money = JSON.parse(JSON.stringify(this.loanCeiling))
            this.value5Year = parseFloat(this.tostring(this.name5))

            this.singleArea3 = [{
                label: 0 + '万',
                children: DataBulk.selecYear
            }]
            this.singleArea5 = [{
                label: this.value5Money + '万',
                children: DataBulk.selecYear
            }]
        }

    }
    // 商业贷款总额年限
    onOk3(result) {

        this.name3 = result[0].label + ' ' + result[1].label
   
        this.value3Money = parseFloat(result[0].label)
        this.value3Year =  parseFloat(result[1].label)

        this.name5 = this.loanCeiling - this.value3Money + '万 ' + result[1].label
        this.value5Money = JSON.parse(JSON.stringify(this.loanCeiling - this.value3Money))
        this.value5Year = parseFloat(result[1].label)

        this.singleArea5 = [{
            label: this.value5Money + '万',
            children: [{label: result[1].label }]
        }]
    }
    // 公积金贷款
    onOk5(result) {

        if(this.isdisabled5 == false) {
            this.name5 = result[0].label + ' ' + result[1].label
            this.value5Money = parseFloat(result[0].label)
            this.value5Year = parseFloat(result[1].label)
    
            this.name3 = ''
            this.value3Money = 0
            this.value3Year = 0
        } 
        
    }
    // 商贷利率
    onOk4(result) {

        this.name4 = result[0]
        this.value4 = this.tonumber(result[0])
    }
    // 公积金利率
    onOk6(result) {

        this.name6 = result[0]
        this.value6 = this.tonumber(result[0])
    }
    // 商业贷款总额原数组
    businessData() {

        var arrs = []
        for (let i = 1; i <= this.loanCeiling - this.value5Money; i++) {

            arrs.push({
                label: i + '万',
                children: DataBulk.selecYear
            })
        };
        return arrs
    }
    // 公积金贷款总额原数组
    gjjData() {

        var arrs = []
        for (let i = 1; i <= this.loanCeiling - this.value3Money; i++) {

            arrs.push({
                label: i + '万',
                children: DataBulk.selecYear
            })
        };

        return arrs
    }
    // 获取'万'后的内容
    tostring(string) {

        let indexs = string.indexOf('万');
        let takeNumber = (string.substring(indexs + 1));
        return takeNumber
    }
    // 获取()内数值
    tonumber(string) {

        let indexs = string.indexOf('(');
        let takeNumber = parseFloat(string.substring(indexs + 1));
        return takeNumber
    }

}
